import { ref, watch, onMounted, Ref } from 'vue'
import axios from '/@/api/axios'
import dftImg from '../../assets/image/default-user.png'
import { useStore } from '/@/store'

export function useUserInfo({ show }: { show: Ref<boolean> }) {
  const userInfo = ref<any>({})
  let defaultImg = ref('')

  const { state } = useStore()

  onMounted(getUserInfo)

  watch(show, (show) => {
    if (!show) {
      getUserInfo()
    }
  })

  async function getUserInfo() {
    const { data } = await axios.get(`/api/blade-user/info`)
    userInfo.value = data.data
    defaultImg.value = dftImg
  }

  return {
    userInfo,
    defaultImg,
  }
}

export function useUserprojectgauge({ userInfo }: { userInfo: Ref<any> }) {
  const projectList = ref<any[]>([])

  watch(userInfo, async (user) => {
    const {
      data: {
        data: { records },
      },
    } = await getProjectList(user.id)
    projectList.value = records
  })

  return {
    projectList,
  }
}

export function getProjectList(userId: number) {
  const url = `/api/blade-measure/measure/userprojectgauge/page?current=1&size=1000&includeId=${userId}`

  return axios.get(url)
}
